<template>
  <div class="carOutbox">
    <a-card class="card">
      <!-- <template #title>
        <a-row align="middle" justify="space-between">
          <span style="font-weight: 700">考生信息管理</span>
          <a-col style="display: flex">
            <a-upload
              :showUploadList="showUploadList"
              accept=".xlsx, .xls, .dbf"
              v-model:file-list="fileList"
              name="file"
              :customRequest="customRequest"
            >
              <a-button
                type="primary"
                v-if="route.meta?.buttonList?.includes('ApplicationInfoindexImport')"
              >
                学生信息导入
              </a-button>
            </a-upload>
            <span style="font-weight: 700">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span>
            <a-upload
              :showUploadList="showExamList"
              accept=".xlsx, .xls, .dbf"
              v-model:file-list="fileList"
              name="file"
              :customRequest="importExamExaminees"
            >
              <a-button
                type="primary"
                v-if="route.meta?.buttonList?.includes('ApplicationInfoindexImport')"
              >
                缺考信息导入
              </a-button>
            </a-upload>
          </a-col>
        </a-row>
      </template> -->
      <a-row align="middle" justify="space-between">
        <SearchGroup
          :label-option="labelOption"
          @handleSearch="handleSearch"
          @handleReset="handleReset"
          :searchOption="filterOption"
        />
      </a-row>

      <a-table
        bordered
        :pagination="false"
        :dataSource="list"
        class="tablstyle"
        size="middle"
        :columns="columns"
        :loading="loading"
        :scroll="{ x: 1450 }"
      >
        <template #bodyCell="{ record, column, index, text }">
          <template v-if="column.dataIndex == 'index'">
            {{ (curPage - 1) * pageSize + (index + 1) }}
          </template>
          <template v-if="column.dataIndex == 'zzllm'">
            <div>
              ({{ record.zzllm ?? '--' }}){{ record.zzllmc ?? '--' }}
              <span style="color: red" v-if="record.zzllFlag && record.zzllFlag !== null">
                (缺考)
              </span>
              <a-popconfirm
                v-if="record.zzllFlag !== null"
                :title="record.zzllFlag ? '确认取消缺考吗?' : '确认设置为缺考吗?'"
                @confirm="handleCancel(record, record.zzllm, 'zzllFlag')"
              >
                <a-button type="link" v-if="!record.zzllFlag"> 设置缺考 </a-button>
                <a-button type="link" v-else>取消缺考</a-button>
              </a-popconfirm>
            </div>
          </template>
          <template v-if="column.dataIndex == 'wgymc'">
            <div>
              ({{ record.wgym ?? '--' }}){{ record.wgymc ?? '--' }}
              <span style="color: red" v-if="record.wgyFlag && record.wgyFlag !== null">
                (缺考)
              </span>
              <a-popconfirm
                v-if="record.wgyFlag !== null"
                :title="record.wgyFlag ? '确认取消缺考吗?' : '确认设置为缺考吗?'"
                @confirm="handleCancel(record, record.wgym, 'wgyFlag')"
              >
                <a-button type="link" v-if="!record.wgyFlag"> 设置缺考 </a-button>
                <a-button type="link" v-else>取消缺考</a-button>
              </a-popconfirm>
            </div>
          </template>
          <template v-if="column.dataIndex == 'ywk1mc'">
            <div>
              ({{ record.ywk1m ?? '--' }}){{ record.ywk1mc ?? '--' }}
              <span style="color: red" v-if="record.ywk1Flag && record.ywk1Flag !== null">
                (缺考)
              </span>
              <a-popconfirm
                v-if="record.ywk1Flag !== null"
                :title="record.ywk1Flag ? '确认取消缺考吗?' : '确认设置为缺考吗?'"
                @confirm="handleCancel(record, record.ywk1m, 'ywk1Flag')"
              >
                <a-button type="link" v-if="!record.ywk1Flag"> 设置缺考 </a-button>
                <a-button type="link" v-else>取消缺考</a-button>
              </a-popconfirm>
            </div>
          </template>
          <template v-if="column.dataIndex == 'ywk2mc'">
            <div>
              ({{ record.ywk2m ?? '--' }}){{ record.ywk2mc ?? '--' }}
              <span style="color: red" v-if="record.ywk2Flag && record.ywk2Flag !== null"
                >(缺考)
              </span>
              <a-popconfirm
                v-if="record.ywk2Flag !== null"
                :title="record.ywk2Flag ? '确认取消缺考吗?' : '确认设置为缺考吗?'"
                @confirm="handleCancel(record, record.ywk2m, 'ywk2Flag')"
              >
                <a-button type="link" v-if="!record.ywk2Flag"> 设置缺考 </a-button>
                <a-button type="link" v-else>取消缺考</a-button>
              </a-popconfirm>
            </div>
          </template>
          <template v-if="column.dataIndex == 'tmzt'">
            {{ text === 'wdy' ? '未打印' : '已打印' }}
          </template>
          <template v-if="column.dataIndex == 'operation'">
            <!-- <a-button type="link" style="color: orange" @click="handleEdit(record)">
              编辑
            </a-button> -->
          </template>
        </template>
      </a-table>
      <a-pagination
        :show-total="total => `共 ${total} 条`"
        :show-size-changer="false"
        show-quick-jumper
        v-model:current="curPage"
        v-model:pageSize="pageSize"
        :total="total"
      />
    </a-card>
  </div>
</template>

<script lang="ts" setup>
import { onMounted, ref, computed } from 'vue'
import { useList } from '/@/hooks/web/useList'
import { uploadApi } from '/@/api/sys/upload'
import {
  getStudentList,
  updateExamineesState,
} from '/@/api/markingStatistics/studentRegistrationInfo'
import { useMessage } from '/@/hooks/web/useMessage'
import AddStudent from './addStudent.vue'

import { useRoute } from 'vue-router'

const route = useRoute()

const { createMessage } = useMessage()
import { useUserStore } from '/@/store/modules/user'
const userStore = useUserStore()
const userInfo = <any>computed(() => userStore.getUserInfo)

const { reset, loadData, filterOption, curPage, total, pageSize, list, loading } = useList(
  getStudentList,
  {
    defaultFilterOption: { bknf: userInfo.value.kmYear },
  }
)

const fileList = ref()
const showUploadList = ref(true)
const showExamList = ref(true)

const kmdmIstrue = ref(true)
const otherKeyIstrue = ref(false)

const labelOption = ref([
  // { name: '考生准考证号', key: 'zkzh', inputWidth: 140 },
  // { name: '考生姓名', key: 'ksxm', inputWidth: 140, disabled: otherKeyIstrue },
  {
    name: '业务课1代码',
    key: 'ywk1m',
    labelWidth: 120,
    inputWidth: 120,
    disabled: otherKeyIstrue,
  },
  {
    name: '业务课2代码',
    key: 'ywk2m',
    labelWidth: 120,
    inputWidth: 120,
    disabled: otherKeyIstrue,
  },
  {
    name: '报考地点',
    key: 'bmdmc',
    labelWidth: 80,
    inputWidth: 150,
    disabled: otherKeyIstrue,
  },
  {
    name: '缺考科目',
    key: 'isMissExam',
    labelWidth: 100,
    selectOption: [
      { label: '政治理论缺考', value: 'zzllFlag' },
      { label: '外国语缺考', value: 'wgyFlag' },
      { label: '业务科1缺考', value: 'ywk1Flag' },
      { label: '业务科2缺考', value: 'ywk2Flag' },
    ],
    inputWidth: 180,
    changefunction: key => {
      delete filterOption.value.ksxm
      delete filterOption.value.ywk1m
      delete filterOption.value.ywk2m
      delete filterOption.value.bmdmc
      delete filterOption.value.tmzt
      delete filterOption.value.zzllFlag
      delete filterOption.value.wgyFlag
      delete filterOption.value.ywk1Flag
      delete filterOption.value.ywk2Flag
      filterOption.value[key] = true
      kmdmIstrue.value = false
      otherKeyIstrue.value = true
    },
  },
  {
    name: '缺考科目代码',
    key: 'kmdm',
    labelWidth: 100,
    inputWidth: 150,
    disabled: kmdmIstrue,
  },
  // {
  //   name: '条码状态',
  //   key: 'tmzt',
  //   labelWidth: 80,
  //   inputWidth: 100,
  //   selectOption: [
  //     { label: '未打印', value: 'wdy' },
  //     { label: '已打印', value: 'ydy' },
  //   ],
  // },
])

const handleSearch = () => {
  if (!kmdmIstrue.value && !filterOption.value.kmdm)
    return createMessage.error('请输入缺考科目代码')
  curPage.value = 1
}

const handleReset = () => {
  kmdmIstrue.value = true
  otherKeyIstrue.value = false
  reset()
}

const columns = [
  {
    title: '序号',
    dataIndex: 'index',
    key: 'index',
    align: 'center',
    width: 80,
    fixed: true,
  },

  // {
  //   title: '考生姓名',
  //   dataIndex: 'ksxm',
  //   key: 'ksxm',
  //   align: 'center',
  // },
  // {
  //   title: '准考证号',
  //   dataIndex: 'zkzh',
  //   key: 'zkzh',
  //   align: 'center',
  // },
  {
    title: '考试地点',
    dataIndex: 'bmdmc',
    key: 'bmdmc',
    align: 'center',
  },
  {
    title: '政治理论名称',
    dataIndex: 'zzllm',
    key: 'zzllm',
    align: 'center',
    width: 200,
  },
  {
    title: '外国语名称',
    dataIndex: 'wgymc',
    key: 'wgymc',
    align: 'center',
    width: 200,
  },
  {
    title: '业务课1码名',
    dataIndex: 'ywk1mc',
    key: 'ywk1mc',
    align: 'center',
    width: 200,
  },
  {
    title: '业务课2码名称',
    dataIndex: 'ywk2mc',
    key: 'ywk2mc',
    align: 'center',
    width: 200,
  },
  {
    title: '报考院系所名称',
    dataIndex: 'bkyxsmc',
    key: 'bkyxsmc',
    align: 'center',
    customRender: ({ record }) => {
      return `(${record.bkyxsm || '--'})${record.bkyxsmc || '--'}`
    },
  },
  {
    title: '报考专业名称',
    dataIndex: 'bkzymc',
    key: 'bkzymc',
    align: 'center',
    customRender: ({ record }) => {
      return `(${record.bkzydm || '--'})${record.bkzymc || '--'}`
    },
  },
  {
    title: '研究方向名称',
    dataIndex: 'yjfxmc',
    key: 'yjfxmc',
    align: 'center',
    customRender: ({ record }) => {
      return `(${record.yjfxm || '--'})${record.yjfxmc || '--'}`
    },
  },
  {
    title: '考试代码',
    dataIndex: 'bkzydm',
    key: 'bkzydm',
    align: 'center',
  },
  // {
  //   title: '条码状态',
  //   dataIndex: 'tmzt',
  //   key: 'tmzt',
  //   align: 'center',
  // },
]
const nodeEnv = import.meta.env.VITE_USER_NODE_ENV

if (nodeEnv == 'JQPY') {
  labelOption.value.push({
    name: '条码状态',
    key: 'tmzt',
    labelWidth: 80,
    inputWidth: 100,
    selectOption: [
      { label: '未打印', value: 'wdy' },
      { label: '已打印', value: 'ydy' },
    ],
    disabled: otherKeyIstrue,
  })

  columns.push({
    title: '条码状态',
    dataIndex: 'tmzt',
    key: 'tmzt',
    align: 'center',
  })
}

// columns.push({
//   title: '操作',
//   dataIndex: 'operation',
//   align: 'center',
//   width: 100,
// });

// 导入学生
const customRequest = async file => {
  showUploadList.value = true
  const fileNames = file.file.name.split('.')
  const fileType = fileNames[fileNames.length - 1].toLocaleLowerCase()
  if (fileType != 'dbf' && fileType != 'xlsx' && fileType != 'xls') {
    createMessage.warning('只能上传.xlsx、.xls或.dbf类型的文件！')
    showUploadList.value = false
    return
  }
  file.year = userInfo.value.kmYear
  const {
    data: { code, message },
  } = await uploadApi(`/ksk/importKskInfo/${userInfo.value.kmYear}`, file)
  if (code === 200) {
    createMessage.success('上传文件成功')
    loadData()
  } else {
    createMessage.error(message)
  }
  showUploadList.value = false
}

// 导入缺考信息
const importExamExaminees = async file => {
  showExamList.value = true
  const fileNames = file.file.name.split('.')
  const fileType = fileNames[fileNames.length - 1].toLocaleLowerCase()
  if (fileType != 'dbf' && fileType != 'xlsx' && fileType != 'xls') {
    createMessage.warning('只能上传.xlsx、.xls或.dbf类型的文件！')
    showExamList.value = false
    return
  }
  file.year = userInfo.value.kmYear
  const {
    data: { code, message },
  } = await uploadApi(`/importExport/importMissExamExaminees/${userInfo.value.kmYear}`, file)
  if (code === 200) {
    createMessage.success('上传文件成功')
    loadData()
  } else {
    createMessage.error(message)
  }
  showExamList.value = false
}

const addVisible = ref(false)

const addStudentForm = ref({})

const handleAdd = () => {
  addVisible.value = true
  addStudentForm.value = {}
}

const handleEdit = row => {
  addVisible.value = row
  addStudentForm.value = {}
}

const handleCancel = async (record, kmdm, key) => {
  try {
    let { code, message } = await updateExamineesState({
      zkzh: record.zkzh,
      kmdm,
      kmYear: record.bknf,
    })
    if (code == 200) {
      createMessage.success('操作成功')
      record[key] = !record[key]
    } else {
      createMessage.error(message)
    }
  } catch (err) {
    console.log(err)
  }
}

onMounted(() => {})
</script>

<style lang="less" scoped>
.carOutbox {
  // height: @c_height;

  .card {
    border-radius: 8px;

    ::v-deep(.ant-card-body) {
      padding-top: 8px !important;
    }
  }
}

.btn {
  margin-left: 20px;
}

.tablstyle {
  padding: 15px 0;
}
</style>
